package game.role;

import game.controller.Entity;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.Image;

import java.util.HashMap;

import static game.basic.GameBase.*;
import static game.basic.Util.getImage;
import static game.main.GameApp.gameMusic;
import static game.main.GameApp.player;

public class Mon_GreenSlime extends Entity {

    static HashMap<Integer, Image[]> monsterMap = new HashMap<>();

    public Mon_GreenSlime() {
        speed = 1;
        maxHp = 4;
        hp = maxHp;

        solidAreaX = 3;
        solidAreaY = 18;
        solidAreaWidth = 42;
        solidAreaHeight = 30;

        name = "史莱姆";

        direction = downDir;

        exp = 3;

        readMonsterImage("/monster/greenslime_down_1.png", tileSize, tileSize);

        attack = 2;
        defense = 1;
    }


    public void readMonsterImage(String url, int width, int height) {
        Image[] up = new Image[ImgFps];
        Image[] down = new Image[ImgFps];
        Image[] left = new Image[ImgFps];
        Image[] right = new Image[ImgFps];

        up[0] = getImage(url, 48, 48);
        up[1] = getImage("/monster/greenslime_down_2.png", width, height);
        down[0] = getImage(url, 48, 48);
        down[1] = getImage("/monster/greenslime_down_2.png", width, height);
        left[0] = getImage(url, 48, 48);
        left[1] = getImage("/monster/greenslime_down_2.png", width, height);
        right[0] = getImage(url, 48, 48);
        right[1] = getImage("/monster/greenslime_down_2.png", width, height);

        monsterMap.put(upDir, up);
        monsterMap.put(downDir, down);
        monsterMap.put(leftDir, left);
        monsterMap.put(rightDir, right);
    }


    @Override
    public void draw(GraphicsContext gc) {
        if ((worldX) + tileSize > player.worldX - screenX &&
                (worldX) - tileSize < player.worldX + screenX &&
                (worldY) + tileSize > player.worldY - screenY &&
                (worldY) - tileSize < player.worldY + screenY) {

            if (hpBarOn) {//绘制血条
                double oneScale = (double) tileSize / maxHp;//得出有几格血
                double hpBarValue = oneScale * hp;

                gc.setFill(cBlack);
                gc.fillRect((worldX) - player.worldX + screenX - 1, (worldY) - player.worldY + screenY - 6, 50, 12);

                gc.setFill(cRed);
                gc.fillRect((worldX) - player.worldX + screenX, (worldY) - player.worldY + screenY - 5, hpBarValue, 10);
                if (++hpBarCounter > 600) {
                    hpBarCounter = 0;
                    hpBarOn = false;
                }
            }

            if (invincible) {
                hpBarOn = true;
                gc.setGlobalAlpha(0.5);
                speed = 3;
                if (++invincibleCounter >= 90) {
                    invincible = false;
                    invincibleCounter = 0;
                    speed = 1;
                }
            } else if (dying) {
                if (++dyingCounter >= MAX_Dying_Counter) {
                    dyingCounter = 0; // 重置dyingCounter或进行其他处理//dying = false;//垂死动画不再播放
                    alive = false;//更改为死亡,清除自己
                    dying = false;
                    return; // 如果不需要进一步绘制，可以返回
                }
                float alpha = ALPHA_VALUES[dyingCounter - 1]; // 注意数组索引从0开始，而dyingCounter从1开始，所以需要减1
                gc.setGlobalAlpha(alpha); // 设置透明度
            }

            gc.drawImage(monsterMap.get(direction)[spriteNum],
                    (worldX) - player.worldX + screenX,
                    (worldY) - player.worldY + screenY);
            gc.setGlobalAlpha(1);
            //gc.strokeRect((worldX) - player.worldX + screenX+solidAreaX,(worldY) - player.worldY + screenY+solidAreaY,solidAreaWidth,solidAreaHeight);
        }
    }

    @Override
    public void update() {
        if (alive && !dying) {
            setAction();
            collisionOn = false;
            cChecker.checkTile(this, currentMap);
            cChecker.checkObject(this, false);
            cChecker.checkEntity(this, npc);
            cChecker.checkEntity(this, monster);
            if (cChecker.checkPlayer(this) && !player.invincible) {
                gameMusic.playSE(6);
                player.invincible = true;
            }
            super.update();
        }
    }

    public void damageReaction() {
        actionLockCounter = 0;
        switch (player.direction) {
            case upAttack -> direction = upDir;
            case downAttack -> direction = downDir;
            case leftAttack -> direction = leftDir;
            case rightAttack -> direction = rightDir;
        }
    }

}
